<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveResource::Validations</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveResource::Validations</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/activeresource/lib/active_resource/validations_rb.html">
                vendor/rails/activeresource/lib/active_resource/validations.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Module to allow validation of <a
href="../ActiveResource.html">ActiveResource</a> objects, which creates an
<a href="Errors.html">Errors</a> instance for every resource. Methods are
implemented by overriding +Base#validate+ or its variants Each of these
methods can inspect the state of the object, which usually means ensuring
that a number of attributes have a certain value (such as not empty, within
a given range, matching a certain regular expression and so on).
</p>
<h4>Example</h4>
<pre>
  class Person &lt; ActiveResource::Base
     self.site = &quot;http://www.localhost.com:3000/&quot;
     protected
       def validate
         errors.add_on_empty %w( first_name last_name )
         errors.add(&quot;phone_number&quot;, &quot;has invalid format&quot;) unless phone_number =~ /[0-9]*/
       end

       def validate_on_create # is only run the first time a new object is saved
         unless valid_member?(self)
           errors.add(&quot;membership_discount&quot;, &quot;has expired&quot;)
         end
       end

       def validate_on_update
         errors.add_to_base(&quot;No changes have occurred&quot;) if unchanged_attributes?
       end
  end

  person = Person.new(&quot;first_name&quot; =&gt; &quot;Jim&quot;, &quot;phone_number&quot; =&gt; &quot;I will not tell you.&quot;)
  person.save                         # =&gt; false (and doesn't do the save)
  person.errors.empty?                # =&gt; false
  person.errors.count                 # =&gt; 2
  person.errors.on &quot;last_name&quot;        # =&gt; &quot;can't be empty&quot;
  person.attributes = { &quot;last_name&quot; =&gt; &quot;Halpert&quot;, &quot;phone_number&quot; =&gt; &quot;555-5555&quot; }
  person.save                         # =&gt; true (and person is now saved to the remote service)
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000799">errors</a>&nbsp;&nbsp;
      <a href="#M000797">save_with_validation</a>&nbsp;&nbsp;
      <a href="#M000798">valid?</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000799" class="method-detail">
        <a name="M000799"></a>

        <div class="method-heading">
          <a href="#M000799" class="method-signature">
          <span class="method-name">errors</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns the <a href="Errors.html">Errors</a> object that holds all
information about attribute error messages.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000799-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000799-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/validations.rb, line 284</span>
284:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">errors</span>
285:       <span class="ruby-ivar">@errors</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">Errors</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword kw">self</span>)
286:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000797" class="method-detail">
        <a name="M000797"></a>

        <div class="method-heading">
          <a href="#M000797" class="method-signature">
          <span class="method-name">save_with_validation</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Validate a resource and save (POST) it to the remote web service.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000797-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000797-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/validations.rb, line 261</span>
261:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">save_with_validation</span>
262:       <span class="ruby-identifier">save_without_validation</span>
263:       <span class="ruby-keyword kw">true</span>
264:     <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">ResourceInvalid</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">error</span>
265:       <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">from_xml</span>(<span class="ruby-identifier">error</span>.<span class="ruby-identifier">response</span>.<span class="ruby-identifier">body</span>)
266:       <span class="ruby-keyword kw">false</span>
267:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000798" class="method-detail">
        <a name="M000798"></a>

        <div class="method-heading">
          <a href="#M000798" class="method-signature">
          <span class="method-name">valid?</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Checks for <a href="Validations.html#M000799">errors</a> on an object
(i.e., is resource.errors empty?).
</p>
<h4>Examples</h4>
<pre>
  my_person = Person.create(params[:person])
  my_person.valid?
  # =&gt; true

  my_person.errors.add('login', 'can not be empty') if my_person.login == ''
  my_person.valid?
  # =&gt; false
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000798-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000798-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activeresource/lib/active_resource/validations.rb, line 279</span>
279:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">valid?</span>
280:       <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">empty?</span>
281:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>